package com.cg.offer;

import org.junit.Test;

/**
 * 剑指Offer 05.替换空格
 *
 * @program: LeetCode->Offer_05
 * @description: 剑指Offer 05.替换空格
 * @author: cg
 * @create: 2022-03-11 15:21
 **/
public class Offer_05 {

    @Test
    public void test05() {
        System.out.println(replaceSpace("We are happy."));
    }

    /**
     * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
     * <p>
     * 示例 1：
     * 输入：s = "We are happy."
     * 输出："We%20are%20happy."
     * <p>
     * 限制：
     * 0 <= s 的长度 <= 10000
     *
     * @param s
     * @return
     */
    public String replaceSpace(String s) {
        StringBuilder res = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c == ' ') {
                res.append("%20");
            } else {
                res.append(c);
            }
        }
        return res.toString();
    }

}
